unit engine;

interface

uses
  Microsoft.DirectX,
  Microsoft.DirectX.Direct3D,
  System.Windows.Forms,
  System.Drawing;

type CEngine = class
  private
    m_Device : Device;
    m_Width, m_Height : integer;

  public
    constructor Create( conteneur : Control );

    procedure SetupCamera(v : vector3);
    procedure StartRender;
    procedure FinishRender;
    function getDevice : Device;

end;
implementation

  constructor CEngine.Create( conteneur : Control );
  var
    presentparam : Array [0..0] of PresentParameters;
  begin
    inherited Create;
    presentparam[0] := PresentParameters.Create;
    presentparam[0].Windowed := true;
    presentparam[0].SwapEffect := SwapEffect.Discard; //comment on switch les backbuffers
    presentparam[0].BackBufferCount := 1;
    presentparam[0].BackBufferWidth := conteneur.Width;
    presentparam[0].BackBufferHeight := conteneur.Height;
    presentparam[0].BackBufferFormat := Microsoft.DirectX.Direct3D.Manager.Adapters.Default.CurrentDisplayMode.Format;

    m_width := conteneur.Width;
    m_height := conteneur.Height;

    m_Device := Device.Create( 0,              // adaptateur : periph graphique
                                DeviceType.Hardware,
                                conteneur,
                                CreateFlags.SoftwareVertexProcessing,
                                presentparam );

    setupCamera(vector3.Create(6, 4, -20));

  end;

  procedure CEngine.StartRender;
  begin
    m_Device.clear( clearflags.Target, Color.Blue, 0, 0);
    m_Device.BeginScene;
    
  end;

  procedure CEngine.FinishRender;
  begin
    m_Device.Endscene;
    m_Device.Present;
  end;

  function CEngine.getDevice : Device;
  begin
    result := m_Device;
  end;

  procedure CEngine.SetupCamera(v : vector3);
  begin
    m_Device.Transform.Projection := Matrix.PerspectiveFovLH( Math.PI / 2, m_width / m_height, 1, 100  );
    m_Device.Transform.View := Matrix.LookAtLH( v, Vector3.Create, Vector3.Create( 0, 1, 0) );
  end;


end.
